package com.cg.asm.body.controller.roles;

import com.alibaba.fastjson.JSONObject;
import com.cg.asm.body.domain.vo.roles.RoleFunPointsVO;
import com.cg.asm.body.domain.vo.roles.RoleFunTreeVO;
import com.cg.asm.body.domain.vo.roles.RoleFunVO;
import com.cg.asm.body.service.roles.IRoleFunService;
import com.cg.asm.client.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "RoleFunController", value = "角色功能清单接口")
@RestController
@RequestMapping("/roleFun")
public class RoleFunController {

    private final IRoleFunService roleFunService;

    public RoleFunController(IRoleFunService roleFunService) {
        this.roleFunService = roleFunService;
    }

    @ApiOperation(value = "获取功能清单树信息", notes = "使用场景:角色分配功能清单")
    @GetMapping("/menuTreeData")
    public JSONObject menuTreeData() {
        List<RoleFunTreeVO> roleFunTreeVOList = roleFunService.getMenuTreeData();
        return CommonUtil.successJson(roleFunTreeVOList);
    }

    @ApiOperation(value = "根据菜单编码获取所有可分配的功能点信息", notes = "角色功能清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "funListCode", value = "功能清单编码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/menuPointsData/{funListCode}")
    public JSONObject menuPointsData(@PathVariable String funListCode) {
        List<RoleFunPointsVO> roleFunTreeVOList = roleFunService.getMenuPointsData(funListCode);
        return CommonUtil.successJson(roleFunTreeVOList);
    }

    @ApiOperation(value = "根据角色ID获取已经选择的功能清单编码", notes = "角色功能清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/roleFunListCodes/{roleId}")
    public JSONObject roleFunListCodes(@PathVariable String roleId) {
        List<String> funListCodes = roleFunService.getRoleFunListCodes(roleId);
        return CommonUtil.successJson(funListCodes);
    }

    @ApiOperation(value = "根据角色Id和功能清单编码获取选中功能点", notes = "角色功能清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "funListCode", value = "功能清单编码", required = true, dataType = "String", paramType = "body")
    })
    @GetMapping("/roleFunPointsCode")
    public JSONObject roleFunPointsCode(String roleId, String funListCode) {
        List<String> funListCodes = roleFunService.getRoleFunPointsCode(roleId, funListCode);
        return CommonUtil.successJson(funListCodes);
    }

    @ApiOperation(value = "还原功能点默认值", notes = "角色功能清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "funListCode", value = "功能清单编码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/restorePointsDefaults/{funListCode}")
    public JSONObject restorePointsDefaults(@PathVariable String funListCode) {
        List<String> funListCodes = roleFunService.restorePointsDefaults(funListCode);
        return CommonUtil.successJson(funListCodes);
    }

    @ApiOperation(value = "保存角色功能清单信息", notes = "角色功能清单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleFunVO", value = "角色功能清单试图对象", required = true, dataType = "RoleFunVO", paramType = "body")
    })
    @PostMapping("/saveRoleMenu")
    public JSONObject saveRoleMenu(@RequestBody RoleFunVO roleFunVO) {
        try {
            roleFunService.saveRoleMenu(roleFunVO);
            return CommonUtil.successJson();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.errorJson();
        }
    }

}
